package org.example;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
//@EnableRetry(proxyTargetClass = true)
@Slf4j
@RestController
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public RetryTemplate getRetryTemplate() {
        RetryTemplate template = RetryTemplate.builder().maxAttempts(3)
                .exponentialBackoff(100, 2, 10000).retryOn(Exception.class)
                .traversingCauses().build();
        return template;
    }

    @Autowired
    private DoRetryService doRetryService;

    @GetMapping("echo")
    public boolean echo() {
        boolean isDone = false;
        try {
            isDone = doRetryService.doRetry(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            isDone = false;
        } finally {
            log.info("isDone : {}", isDone);
        }
        return isDone;
    }


}
